Public Class frmError

    Private _ErrorText, _FullMsg As String, _Ex As Exception, _LogFile As String

    Public Sub New(ByVal ErrorText As String, ByVal Ex As Exception)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        If Me.DesignMode Then Exit Sub

        With GetAssembly.GetName.Version
            lblHeading.Text = StringFormat("An error has occurred in WRDB at {0}", Now.ToString(MyDateTimeFormat))
            lblVersion.Text = StringFormat("Assembly: {1}; Version: {0}", .ToString(4), System.Reflection.Assembly.GetExecutingAssembly.GetName.Name)
        End With
        _ErrorText = ErrorText
        _Ex = Ex
        _LogFile = IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) & "\Error.log"
    End Sub

    Private Sub frmError_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Me.DesignMode Then Exit Sub

        Dim dblline As String = "".PadRight(80, "=")
        Dim line As String = "".PadRight(80, "_")
        txtErrorMsg.Text = _ErrorText.Replace("\n", vbCrLf).Replace("\t", vbTab)
        _FullMsg = String.Format("{0}\n\n{1}\n\n{2}\n\n{3}\n\n", dblline, lblHeading.Text, lblVersion.Text, _ErrorText).Replace("\n", vbCrLf).Replace("\t", vbTab)

        If _Ex Is Nothing Then
            lnkDetail.Visible = False
        Else
            txtErrorMsg.Text &= String.Format("\n\nError Message:\n\n{0}", _Ex.Message.Replace(vbCrLf, vbCr).Replace(vbCr, vbCrLf)).Replace("\n", vbCrLf)
            With GetAssembly()
                _FullMsg &= String.Format("{0}\n\nInformation about the executing assembly:\n\nFull Name: {1}\nVersion: {2}\nLast Updated: {3}\n\n",
                                          line, .Location, .GetName.Version.ToString, IO.File.GetLastWriteTime(.Location).ToString(MyDateTimeFormat)).Replace("\n", vbCrLf)
            End With
            _FullMsg &= String.Format("{0}\n\nThe detailed error message was:\n\n{1}\n\n", line, _Ex.ToString).Replace("\n", vbCrLf)
        End If

        Dim sw As New IO.StreamWriter(_LogFile, True)
        sw.WriteLine(_FullMsg)
        sw.Close()
        sw.Dispose()

        btnClose.Select()
    End Sub

    Private Sub lnkCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCopy.Click
        Try
            Clipboard.SetDataObject(_FullMsg, False)
        Catch ex2 As Exception
            MessageBox.Show("Unable to copy to clipboard.", "WRDB Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub lnkDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDetail.Click
        MessageBox.Show(StringFormat(_Ex.ToString), "WRDB Error Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub lnkLogFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLogFile.Click
        Process.Start(_LogFile)
    End Sub
End Class